package com.leyou.auth.config;

import com.leyou.common.auth.utils.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * @author 虎哥
 */
@Slf4j
@Data
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties implements InitializingBean {
    /**
     * 公钥的路径
     */
    private String pubKeyPath;
    /**
     * 私钥的路径
     */
    private String priKeyPath;
    /**
     * 加载公钥对象
     */
    private PublicKey publicKey;
    /**
     * 加载私钥对象
     */
    private PrivateKey privateKey;


    private UserTokenProperties user = new UserTokenProperties();

    @Data
    public class UserTokenProperties {
        private int expireMinutes;
        private String cookieName;
        private String domain;
        private long minRefreshTime;
    }

    private AppTokenProperties app = new AppTokenProperties();

    @Data
    public class AppTokenProperties {
        private int expireMinutes;
        private long id;
        private String headerName;
    }


//    @PostConstruct
//    public void init(){
//        try {
//            this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
//            this.privateKey = RsaUtils.getPrivateKey(priKeyPath);
//        } catch (Exception e) {
//            log.error("公钥或私钥加载失败！原因：[{}]", e.getMessage(), e);
//            throw new RuntimeException(e);
//        }
//    }

    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
            this.privateKey = RsaUtils.getPrivateKey(priKeyPath);
        } catch (Exception e) {
            log.error("公钥或私钥加载失败！原因：[{}]", e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }
}
